package com.spcloud.lee.thread;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class ThreadDemo2 {
    public static final Object lock = new Object();
//    private static volatile List<String> list = new ArrayList<>();
    public static void main(String[] args) throws InterruptedException {
        List<String> list = Collections.synchronizedList(new ArrayList<>());
        ThreadA tA = new ThreadA(list);
        ThreadB tB = new ThreadB(list);

        tA.start();
        TimeUnit.MILLISECONDS.sleep(100);
        tB.start();

    }

    static class ThreadA extends Thread {

        private List<String> result;

        public ThreadA(List<String> result) {
            this.result = result;
        }


        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("线程" + Thread.currentThread().getName()
                        + "获取到了锁...");
                try {
                    System.out.println("线程" + Thread.currentThread().getName()
                            + "阻塞并释放锁...");
                    lock.wait();
                } catch (InterruptedException e) {
                }
                System.out.println("线程" + Thread.currentThread().getName()
                        + "执行完成...");
            }


        }
    }

    static class ThreadB extends  Thread {
        private List<String> result;

        public ThreadB(List<String> result) {
            this.result = result;
        }

        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("线程" + Thread.currentThread().getName()
                        + "获取到了锁...");
                lock.notify();
                System.out.println("线程" + Thread.currentThread().getName()
                        + "唤醒了正在wait的线程...");
            }
            System.out
                    .println("线程" + Thread.currentThread().getName() + "执行完成...");

        }
    }

}


